//
//  FirstViewController.m
//  test
//
//  Created by 任文超 on 14-8-4.
//  Copyright (c) 2014年 任文超. All rights reserved.
//

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        NSLog(@"init First");
        
        // 导航标题
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((sizeMain.width-160)/2, 0, 160, 40)];
        titleLabel.text = @"first";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor whiteColor];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        [titleLabel setFont: [UIFont fontWithName:@"Helvetica-Bold" size:21]];
        self.navigationItem.titleView = titleLabel;

        // 左按钮
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back~" style:UIBarButtonItemStylePlain target:self action:nil];
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"left" style:UIBarButtonItemStyleBordered target:self action:@selector(clickLeftButton)];
        [leftButton setTintColor:[UIColor whiteColor]];
        
        self.navigationItem.leftBarButtonItem = leftButton;
        
        // 右按钮
        UIBarButtonItem *rightButton1 = [[UIBarButtonItem alloc] initWithTitle:@"right1" style:UIBarButtonItemStyleBordered target:self action:@selector(clickRightButton1)];
        [rightButton1 setTintColor:[UIColor whiteColor]];
        
        UIBarButtonItem *rightButton2 = [[UIBarButtonItem alloc] initWithTitle:@"right2" style:UIBarButtonItemStyleBordered target:self action:@selector(clickRightButton2)];
        [rightButton2 setTintColor:[UIColor whiteColor]];
        
        UIBarButtonItem *rightButton3 = [[UIBarButtonItem alloc] initWithTitle:@"right3" style:UIBarButtonItemStyleBordered target:self action:@selector(clickRightButton3)];
        [rightButton3 setTintColor:[UIColor whiteColor]];
        
        self.navigationItem.rightBarButtonItems = @[rightButton1, rightButton2, rightButton3];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.mycollectionview];
}

- (UICollectionView*)mycollectionview {
    if (!_mycollectionview) {
        MyCollectionViewFlowLayout *layout = [MyCollectionViewFlowLayout new];
        
        _mycollectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
        _mycollectionview.backgroundColor = [UIColor clearColor];
        _mycollectionview.translatesAutoresizingMaskIntoConstraints = NO;
        _mycollectionview.delegate = self;
        _mycollectionview.dataSource = self;
        [_mycollectionview registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MyCollectionViewCell class])];
        [_mycollectionview registerClass:[MyCollectionReusableViewHead class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([MyCollectionReusableViewHead class])];
        [_mycollectionview registerClass:[MyCollectionReusableViewFoot class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([MyCollectionReusableViewFoot class])];
        
    }
    
    return _mycollectionview;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 12;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *cellIdentifier = @"MyCollectionViewCell";
    if (!cellIdentifier) {
        cellIdentifier = @"MyCollectionViewCell";
    }
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    [CATransaction setDisableActions:YES];
    NSLog(@"kind:%@", kind);
    if (kind == UICollectionElementKindSectionHeader) {
      MyCollectionReusableViewHead *viewHead = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([MyCollectionReusableViewHead class]) forIndexPath:indexPath];
        return viewHead;
    }
    else if (kind == UICollectionElementKindSectionFooter) {
        MyCollectionReusableViewFoot *viewFoot = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([MyCollectionReusableViewFoot class]) forIndexPath:indexPath];
        return viewFoot;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

@end
